<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<view class="sugg-list" v-if="searchResults.length">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="goToDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="forward" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="goToGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null, //输入框防抖定时器
				kw: '', //关键词
				searchResults: [], //搜索建议
				historyList: [], //搜索历史
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]');
		},
		computed: {
			//反转搜索历史
			histories(){
				return [...this.historyList].reverse();
			},
		},
		methods: {
			//搜索框输入事件
			input(e){
				clearTimeout(this.timer);
				
				this.timer = setTimeout(() => {
					this.kw = e;
					this.getSearchResults();
				}, 500);
			},
			//获取搜索建议
			async getSearchResults(){
				if(!this.kw){
					//关键词为空
					this.searchResults = [];
					return;
				}
				
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				});
				
				if(res.meta.status !== 200){
					//请求失败
					return uni.$showMsg();
				}
				
				this.searchResults = res.message;
				this.saveSearchHistory();
			},
			//跳转至商品详情页
			goToDetail(item){
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				});
			},
			//将关键词存入搜索历史
			saveSearchHistory(){
				// this.historyList.push(this.kw);
				
				const set = new Set(this.historyList);
				
				set.delete(this.kw);
				set.add(this.kw);
				this.historyList = Array.from(set);
				uni.setStorageSync('kw', JSON.stringify(this.historyList));
			},
			//清空搜索历史
			clean(){
				this.historyList = [];
				uni.setStorageSync('kw', '[]');
			},
			//点击搜索历史
			goToGoodsList(kw){
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw,
				});
			},
		},
	}
</script>

<style lang="scss">
.search-box {
	position: sticky;
	top: 0;
	z-index: 999;
}
.sugg-list {
	padding: 0 5px;
	.sugg-item {
		padding: 13px 0;
		border-bottom: solid 1px #EFEFEF;
		font-size: 12px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.goods-name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}
.history-box {
	padding: 0 5px;
	.history-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 40px;
		border-bottom: solid 1px #EFEFEF;
		font-size: 13px;
	}
	.history-list {
		display: flex;
		flex-wrap: wrap;
		padding-top: 5px;
		.uni-tag {
			margin-right: 5px;
			margin-bottom: 5px;
		}
	}
}
</style>
